package org.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.example.domain.brand;
import org.example.domain.code;
import org.example.domain.result;
import org.example.service.brandservoce;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//创建记录日志的对象
// log.info("用户[{}]权限修改成功，新权限[{}]", userId, permissions);
//用于记录程序干了哪些事，方便追溯
//debug,warn,error是对于不同重要程度的事进行区别
@Slf4j
//是 @Controller + @ResponseBody 的组合注解，用于 RESTful 接口，
// 类中所有方法的返回值都会被自动转为 HTTP 响应体（默认按 JSON 处理）。
@RestController
@RequestMapping("/brand")
public class brandcontroller {

    @Autowired
    private brandservoce brandservoce;
    @GetMapping
    public result selectall(){
List<brand>brands= brandservoce.list();
        Integer code1= brands !=null?code.get_ok:code.get_false;
        String msg=brands!=null?"":"数据查询失败";
        //log.info(msg);
        return new result(brands,code1,msg) ;
    }
    @GetMapping("/{id}")
    public result selectbyid(@PathVariable Integer id){
        brand brand1=brandservoce.getById(id);
        Integer code1= brand1 !=null?code.get_ok:code.get_false;
        String msg=brand1!=null?"":"数据查询失败";
        return new result(brand1,code1,msg) ;

    }
    @PostMapping
    public result insert(@RequestBody brand brand){
boolean flag=brandservoce.save(brand);
return new result(flag,flag? code.save_ok:code.save_false);
    }
    @PutMapping
    public result update(@RequestBody brand brand){
        boolean flag=brandservoce.updateById(brand);
        return new result(flag,flag? code.update_ok:code.update_false);
    }
    @DeleteMapping("/{id}")
    public result delete(@PathVariable Integer id){
        boolean flag=brandservoce.removeById(id);
        return new result(flag,flag? code.delete_ok:code.delete_false);
    }
//    @GetMapping("/{currentpage}/{pagesize}")
//    public result getpage(@PathVariable Integer currentpage,@PathVariable Integer pagesize){
//
//        IPage page=brandservoce.getPage(currentpage,pagesize);
//        Integer code1= page !=null?code.get_ok:code.get_false;
//        String msg=page!=null?"":"分页数据查询失败";
//        return new result(page,code1,msg) ;
//    }

    @GetMapping("/{currentpage}/{pagesize}")
    public result getpage(@PathVariable Integer currentpage,@PathVariable Integer pagesize,brand brand){

        IPage page=brandservoce.getPage(currentpage,pagesize,brand);
        Integer code1= page !=null?code.get_ok:code.get_false;
        String msg=page!=null?"":"分页数据查询失败";
        return new result(page,code1,msg) ;
    }
}
